﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.Core.Presentation;
using ICSharpCode.Core;
using System.Windows;
using System.Windows.Input;
using System.Threading;

namespace MyWPFWorkBench.FluentRibbon
{
    sealed class RibbonButton : Fluent.Button, IStatusUpdate
    {
        readonly Codon codon;
        readonly object caller;
        readonly string inputGestureText;
        readonly IEnumerable<ICondition> conditions;

        public RibbonButton(UIElement inputBindingOwner, Codon codon, object caller, bool createCommand, IEnumerable<ICondition> conditions)
        {

            this.codon = codon;
            this.caller = caller;
            this.Command = CommandWrapper.GetCommand(codon, caller, createCommand, conditions);
            if (codon.Properties.Contains("icon"))
            {
                var image = PresentationResourceService.GetImage(StringParser.Parse(codon.Properties["icon"]));
                image.SetResourceReference(FrameworkElement.StyleProperty, ToolBarService.ImageStyleKey);
                this.SizeDefinition = "Small,Middle";
                this.Icon = image;
            }
            if (codon.Properties.Contains("largeicon"))
            {
                var image = PresentationResourceService.GetImage(StringParser.Parse(codon.Properties["largeicon"]));
                image.SetResourceReference(FrameworkElement.StyleProperty, ToolBarService.ImageStyleKey);
                this.LargeIcon = image;
                this.SizeDefinition = "Large";
            }
            this.conditions = conditions;

            if (codon.Properties.Contains("name"))
            {
                this.Name = codon.Properties["name"];
            }

            if (!string.IsNullOrEmpty(codon.Properties["shortcut"]))
            {
                Fluent.KeyTip.SetKeys(this, codon.Properties["shortcut"]);
            }
            UpdateText();
            
        }

        string GetFeatureName()
        {
            string commandName = codon.Properties["command"];
            if (string.IsNullOrEmpty(commandName))
            {
                return codon.Properties["class"];
            }
            else
            {
                return commandName;
            }
        }

        protected override void OnClick()
        {
            string feature = GetFeatureName();
            if (!string.IsNullOrEmpty(feature))
            {
                AnalyticsMonitorService.TrackFeature(feature, "Toolbar");
            }
            base.OnClick();
        }

        public void UpdateText()
        {
            if (codon.Properties.Contains("label"))
            {
                this.Header = StringParser.Parse(codon.Properties["label"]);
            }
            if (codon.Properties.Contains("tooltip"))
            {
                string toolTip = StringParser.Parse(codon.Properties["tooltip"]);
                if (!string.IsNullOrEmpty(inputGestureText))
                    toolTip = toolTip + " (" + inputGestureText + ")";
                this.ToolTip = toolTip;
            }
        }

        public void UpdateStatus()
        {
            if (ICSharpCode.Core.Condition.GetFailedAction(conditions, caller) == ConditionFailedAction.Exclude)
                this.Visibility = Visibility.Collapsed;
            else
                this.Visibility = Visibility.Visible;
        }
    }
}
